package cn.iocoder.devlop.module.annual.report.sfclicenceinfo.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.devlop.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.devlop.module.annual.report.baseinfo.dal.dataobject.BaseinfoDO;
import cn.iocoder.devlop.module.annual.report.baseinfo.service.BaseinfoService;
import cn.iocoder.devlop.module.annual.report.common.service.EnttypeEntclassifyService;
import cn.iocoder.devlop.module.annual.report.marpripinfo.dal.dataobject.MarpripinfoDO;
import cn.iocoder.devlop.module.annual.report.marpripinfo.service.MarpripinfoService;
import cn.iocoder.devlop.module.annual.report.sfclicenceinfo.controller.vo.SfcLicenceinfoCreateReqVO;
import cn.iocoder.devlop.module.annual.report.sfclicenceinfo.controller.vo.SfcLicenceinfoOverwriteVO;
import cn.iocoder.devlop.module.annual.report.sfclicenceinfo.convert.SfcLicenceinfoConvert;
import cn.iocoder.devlop.module.annual.report.sfclicenceinfo.dal.SfcLicenceinfoMapper;
import cn.iocoder.devlop.module.annual.report.sfclicenceinfo.dal.dataobject.SfcLicenceinfoDO;
import cn.iocoder.devlop.module.annual.report.utils.IdGenerateUtil;
import cn.iocoder.devlop.module.annual.report.utils.ObjectComparisonUtil;
import cn.iocoder.devlop.module.annual.report.utils.ReportYearHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

/**
 * 年报农合行政许可信息 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class SfcLicenceinfoServiceImpl implements SfcLicenceinfoService {

    @Resource
    private SfcLicenceinfoMapper sfcLicenceinfoMapper;

    @Resource
    private MarpripinfoService marpripinfoService;

    @Resource
    private EnttypeEntclassifyService enttypeEntclassifyService;

    @Resource
    private BaseinfoService baseinfoService;

    @Override
    public List<SfcLicenceinfoDO> getSfcLicenceinfoList(String regno, String ancheyear) {
        ReportYearHelper.setYear(NumberUtil.parseInt(ancheyear));
        String ancheid = IdGenerateUtil.getAncheid(regno, ancheyear);
        return sfcLicenceinfoMapper.selectListByAncheid(ancheid);
    }

    @Override
    public void deleteSfcLicenceinfoAll(String regno, String ancheyear) {
        ReportYearHelper.setYear(NumberUtil.parseInt(ancheyear));
        String ancheid = IdGenerateUtil.getAncheid(regno, ancheyear);
        LambdaQueryWrapperX<SfcLicenceinfoDO> wrapperX = new LambdaQueryWrapperX<>();
        wrapperX.eq(SfcLicenceinfoDO::getAncheid,ancheid);
        sfcLicenceinfoMapper.delete(wrapperX);
    }

    @Override
    public void createSfcLicenceinfoByList(String regno, String ancheyear, List<SfcLicenceinfoCreateReqVO> sfcLicenceinfoList) {
        String ancheid = IdGenerateUtil.getAncheid(regno, ancheyear);
        MarpripinfoDO marpripinfo = marpripinfoService.getMarpripinfo(regno);
        String entclassify = enttypeEntclassifyService.getEntclassify(marpripinfo.getEnttype());
        if (CollUtil.isNotEmpty(sfcLicenceinfoList)) {
            List<SfcLicenceinfoDO> sfcLicenceinfoDOS = SfcLicenceinfoConvert.INSTANCE.convertList01(sfcLicenceinfoList);
            for (SfcLicenceinfoDO sfcLicenceinfo : sfcLicenceinfoDOS) {
                sfcLicenceinfo.setEntclassify(entclassify);
                sfcLicenceinfo.setAncheid(ancheid);
                sfcLicenceinfo.setLicid(IdUtil.simpleUUID());
            }
            ReportYearHelper.setYear(NumberUtil.parseInt(ancheyear));
            sfcLicenceinfoMapper.insertBatch(sfcLicenceinfoDOS);
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void overwriteSfcLicenceinfo(SfcLicenceinfoOverwriteVO updateReqVO) {
        BaseinfoDO baseinfo = baseinfoService.getBaseinfo(updateReqVO.getRegno(), updateReqVO.getAncheyear());
        if (ObjUtil.isNotEmpty(baseinfo)){
            if (StrUtil.equals(baseinfo.getState(),"02") ||StrUtil.equals(baseinfo.getState(),"03")) {
                List<SfcLicenceinfoDO> oldSfcLicenceinfoList = getSfcLicenceinfoList(updateReqVO.getRegno(), updateReqVO.getAncheyear());
                List<SfcLicenceinfoCreateReqVO> newAlterstockinfoList = updateReqVO.getSfcLicenceinfoList();
                List<SfcLicenceinfoCreateReqVO> oldreqs = SfcLicenceinfoConvert.INSTANCE.convertList3(oldSfcLicenceinfoList);
                ObjectComparisonUtil.compareObjectList(oldreqs,newAlterstockinfoList,IdGenerateUtil.getAncheid(updateReqVO.getRegno(), updateReqVO.getAncheyear()),"股权变更");
            }
        }
        deleteSfcLicenceinfoAll(updateReqVO.getRegno(), updateReqVO.getAncheyear());
        createSfcLicenceinfoByList(updateReqVO.getRegno(), updateReqVO.getAncheyear(), updateReqVO.getSfcLicenceinfoList());
        baseinfoService.updateState(updateReqVO.getRegno(), updateReqVO.getAncheyear());
    }

}
